package com.gimi.cloud.bbp.manage.service.mall.impl;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.ReflectUtils;
import com.gimi.cloud.bbp.dao.dao.mall.MallAppBannerDao;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.bbp.dto.mall.MallAppBannerDTO;
import com.gimi.cloud.bbp.entity.mall.MallAppBannerEntity;
import com.gimi.cloud.bbp.manage.service.mall.MallAppBannerService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Banner管理相关接口实现类
 *
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-01-30
 **/
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class MallAppBannerServiceImpl implements MallAppBannerService {
    private static final Logger logger = LoggerFactory.getLogger(MallAppBannerServiceImpl.class);
    private MallAppBannerDao mallAppBannerDao;

    @Autowired
    public MallAppBannerServiceImpl(MallAppBannerDao mallAppBannerDao) {
        this.mallAppBannerDao = mallAppBannerDao;
    }

    @Override
    public List<MallAppBannerDTO> queryList(MallAppBannerDTO dto, boolean isPaging) throws Exception {
        if (isPaging){
            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
        }
        String sql = "SELECT ban.* FROM mall_app_banner ban WHERE ban.is_delete = 0 AND ban.abg_id = :abgId AND ban.brp_status = :brpStatus AND ban.abg_id = :abgId  ORDER BY ban.brp_sort";
        QueryParam param = new QueryParam();
        param.setSql(sql);
        param.setParamObject(dto);
        return mallAppBannerDao.selectListDTOBySql(param);
    }

    @Override
    public MallAppBannerDTO load(MallAppBannerDTO dto, Object id) throws Exception {
        MallAppBannerEntity sysRoleEntity = mallAppBannerDao.queryBy("abr_id",id);
        if(null == sysRoleEntity){
            return null;
        }
        MallAppBannerDTO bannerDTO = new MallAppBannerDTO();
        ReflectUtils.copyPropertiesIgnoreNull(sysRoleEntity,bannerDTO);
        return bannerDTO;
    }

    @Override
    public MallAppBannerDTO saveOrUpdate(MallAppBannerDTO dto) throws Exception {
        MallAppBannerEntity banner = null;
        if (null != dto.getAbrId()) {
            banner = mallAppBannerDao.queryBy("abr_id", dto.getAbrId());
            if (null == banner) {
                throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取Bannerid为" + dto.getAbrId() + "的Banner信息，更新失败");
            }
        } else {
            banner = new MallAppBannerEntity();
        }
        ReflectUtils.copyPropertiesIgnoreNull(dto, banner);
        if (null != dto.getAbrId()) {
            mallAppBannerDao.baseUpdateByPrimaryKey(banner);
        } else {
            mallAppBannerDao.baseInsert(banner);
        }
        ReflectUtils.copyPropertiesIgnoreNull(banner, dto);
        return dto;
    }

    @Override
    public int delete(MallAppBannerDTO dto, Object id, boolean realDel) throws Exception {
        MallAppBannerEntity banner = mallAppBannerDao.queryBy("abr_id", id);
        if (null == banner) {
            throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取Bannerid为" + id + "的Banner信息，删除失败");
        }
        //非物理删除
        if(!realDel){
            ReflectUtils.copyPropertiesIgnoreNull(dto,banner);
            banner.setIsDelete(1);
            return mallAppBannerDao.baseUpdateByPrimaryKey(banner);
        }
        return mallAppBannerDao.deleteByPrimaryKey(id);
    }

    @Override
    public Integer deleteBannerByIds(MallAppBannerDTO dto, List<Integer> bannerIds, Integer abgId) throws Exception {
        String bannerId = "";
        for (Integer str : bannerIds) {
            bannerId += str + ",";
        }
        String sql = "UPDATE mall_app_banner SET is_delete = 1,update_uid = '" + dto.getUid()
                + "',update_uname='" + dto.getUname() + "',update_time =NOW() " +
                " WHERE abg_id =" + dto.getAbgId();
        if(bannerId.length() >0){
            bannerId = bannerId.substring(0, bannerId.length() - 1);
            sql=sql+  " AND abr_id not in(" + bannerId + ")";
        }
        QueryParam queryParam = new QueryParam();
        queryParam.setSql(sql);
        return mallAppBannerDao.updateSql(queryParam);
    }
}
